
import initState from "./state";
import compilerToFunction from "./compiler/index";
import { mountComponent } from "./lifeCycle";
import Watcher from "./observer/watcher";
import { callHook } from "./lifeCycle";
import { mergeOptions } from "./util";
export default function initMixin(Vue) {
    Vue.prototype._init = function (options) {
        const vm = this

        vm.$options = mergeOptions(this.constructor.options, options)
        callHook(vm, 'beforeCreate')
        initState(vm)
        callHook(vm, 'created')

        if (vm.$options.el) {
            vm.$mount(vm.$options.el)
        }


    }



    Vue.prototype.$mount = function (el) {
        const vm = this
        let opt = vm.$options
        el = document.querySelector(el)
        let template;
        if (!opt.render) {
            template = opt.template
            if (!opt.template) {
                template = el.outerHTML // 将dom元素转化为str包含本身
            }
        } else {
            template = opt.render
        }


        vm.$el = el

        opt.render = compilerToFunction(template)

        mountComponent(vm)

    }

    Vue.prototype.$watch = function (expOrFn, cb, options = {}) {
        options.user = true
        const watcher = new Watcher(this, expOrFn, options, cb,)
        if (options.immediate) {
            cb.call(vm, watcher.value)
        }
    }

}


